package com.cyy.daijia.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.cyy.daijia.common.constant.MqConst;
import com.cyy.daijia.common.service.RabbitService;
import com.cyy.daijia.driver.client.DriverAccountFeignClient;
import com.cyy.daijia.model.entity.payment.PaymentInfo;
import com.cyy.daijia.model.enums.TradeType;
import com.cyy.daijia.model.form.driver.TransferForm;
import com.cyy.daijia.model.form.payment.PaymentInfoForm;
import com.cyy.daijia.model.vo.order.OrderRewardVo;
import com.cyy.daijia.model.vo.payment.WxPrepayVo;
import com.cyy.daijia.order.client.OrderInfoFeignClient;
import com.cyy.daijia.payment.mapper.PaymentInfoMapper;
import com.cyy.daijia.payment.service.WxPayService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
@RequiredArgsConstructor
public class WxPayServiceImpl implements WxPayService {
    private final OrderInfoFeignClient orderInfoFeignClient;
    private final DriverAccountFeignClient driverAccountFeignClient;
    private final PaymentInfoMapper paymentInfoMapper;
    private final RabbitService rabbitService;
    @Override
    public boolean queryPayStatus(String orderNo) {
        orderNo="bf9c620a8f1c42adb86c86e4cfb8259c";
        handlePayment(orderNo);
        return true;
    }
    @GlobalTransactional
    @Override
    public void handleOrder(String orderNo) {
        orderInfoFeignClient.updateOrderPayStatus(orderNo);

        //2 远程调用：获取系统奖励，打入到司机账户
        OrderRewardVo orderRewardVo = orderInfoFeignClient.getOrderRewardFee(orderNo).getData();
        if(orderRewardVo != null && orderRewardVo.getRewardFee().doubleValue()>0) {
            TransferForm transferForm = new TransferForm();
            transferForm.setTradeNo(orderNo);
            transferForm.setTradeType(TradeType.REWARD.getType());
            transferForm.setContent(TradeType.REWARD.getContent());
            transferForm.setAmount(orderRewardVo.getRewardFee());
            transferForm.setDriverId(orderRewardVo.getDriverId());
            driverAccountFeignClient.transfer(transferForm);
        }
            //1 更新支付记录，状态修改为 已经支付
    }

    @Override
    public WxPrepayVo createWxPayment(PaymentInfoForm paymentInfoForm) {
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, paymentInfoForm.getOrderNo()));
        if(null == paymentInfo) {
            paymentInfo = new PaymentInfo();
            BeanUtils.copyProperties(paymentInfoForm, paymentInfo);
            paymentInfo.setPaymentStatus(0);
            paymentInfoMapper.insert(paymentInfo);
        }
        return new WxPrepayVo("1","2","3","4","5","6");
    }

    public void handlePayment(String orderNo) {

        //1 更新支付记录，状态修改为 已经支付
        //订单编号
        //根据订单编号查询支付记录
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfo::getOrderNo,orderNo);
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(wrapper);
        //如果已经支付，不需要更新
        if(paymentInfo.getPaymentStatus() == 1) {
            return;
        }
        paymentInfo.setPaymentStatus(1);
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setTransactionId("1");
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString("transaction"));
        paymentInfoMapper.updateById(paymentInfo);

        //2 发送端：发送mq消息，传递 订单编号
        //  接收端：获取订单编号，完成后续处理
        rabbitService.sendMessage(MqConst.EXCHANGE_ORDER,
                MqConst.ROUTING_PAY_SUCCESS,
                orderNo);
    }
}
